
//
//  SendVerifyViewController.m
//  UILbrary
//
//  Created by mac on 16/11/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SendVerifyViewController.h"

@interface SendVerifyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *sendMessageLabel;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel; /**< 这个label的作用只是为了给sendMessageLabel提供背景色 */
@property (nonatomic, strong) NSTimer *myTime;

@end

@implementation SendVerifyViewController


#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];


}

#pragma mark - target method

- (IBAction)p_sendMessageMethod:(id)sender {
    //关闭label 的用户响应
    self.sendMessageLabel.userInteractionEnabled = NO;
    [self.myTime fire];
}

#pragma mark - private method

- (void)p_refreshLabelTitle{
    
    //当倒计时开始后，改变label的背景色，增强用户体验
    self.sendMessageLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    static NSInteger cout = 60;
    cout--;
    if (cout<=0) {
        
        //结束倒计时
        [self.myTime invalidate];
        self.myTime = nil;
        
        cout = 60;
        //打开用户响应
        self.sendMessageLabel.userInteractionEnabled = YES;
        //倒计时结束后，label背景色恢复点击之前的颜色，增强用户体验
        self.sendMessageLabel.backgroundColor = self.bottomLabel.backgroundColor;
        self.sendMessageLabel.text = @"获取验证码";
        
    }else{
        self.sendMessageLabel.text = [NSString stringWithFormat:@"获取验证码%ld",cout];
    }
}

#pragma  mark - lazy method

- (NSTimer *)myTime{
    if (!_myTime) {
        _myTime = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(p_refreshLabelTitle) userInfo:nil repeats:YES];
    }
    return _myTime;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
